package com.pan.admin.module.channel.controller;

import com.pan.admin.common.annotation.Log;
import com.pan.admin.common.core.controller.BaseController;
import com.pan.admin.common.core.domain.AjaxResult;
import com.pan.admin.common.core.page.TableDataInfo;
import com.pan.admin.common.enums.BusinessType;
import com.pan.admin.common.utils.poi.ExcelUtil;
import com.pan.admin.module.channel.entity.LoanChannelFormField;
import com.pan.admin.module.channel.entity.vo.ElementTreeVO;
import com.pan.admin.module.channel.service.LoanChannelFormFieldService;
import com.pan.admin.module.loanConfig.entity.LoanFormField;
import com.pan.admin.module.loanConfig.service.LoanFormFieldService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * 渠道关联动态单组Controller
 *
 * @author ts
 * @date 2023-09-15
 */
@RestController
@RequestMapping("/channel/form/field")
public class LoanChannelFormFieldController extends BaseController {

    @Autowired
    private LoanChannelFormFieldService loanChannelFormFieldService;

    @Autowired
    private LoanFormFieldService loanFormFieldService;

    /**
     * 查询渠道关联动态单组列表
     */
    @PreAuthorize("@ss.hasPermi('channel:form:field')")
    @GetMapping("/list")
    public TableDataInfo list(LoanChannelFormField loanChannelFormField) {
        startPage();
        List<LoanChannelFormField> list = loanChannelFormFieldService.list(loanChannelFormField);
        return getDataTable(list);
    }

    /**
     * 查询ids列表
     */
    @PreAuthorize("@ss.hasPermi('channel:form:field')")
    @GetMapping("/choose/treeList")
    public TableDataInfo chooseTreeList(LoanChannelFormField loanChannelFormField) {
        List list = loanChannelFormFieldService.treeList(loanChannelFormField);
        return getDataTable(list);
    }


    /**
     * 动态表单字段树控
     */
    @PreAuthorize("@ss.hasPermi('channel:form:field')")
    @GetMapping("/treeList")
    public TableDataInfo treeList(LoanFormField loanFormField) {
        List<ElementTreeVO> list = loanFormFieldService.treeList(loanFormField);
        return getDataTable(list);
    }

    /**
     * 导出渠道关联动态单组列表
     */
    @PreAuthorize("@ss.hasPermi('channel:form:field:export')")
    @Log(title = "渠道关联动态单组", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LoanChannelFormField loanChannelFormField) {
        List<LoanChannelFormField> list = loanChannelFormFieldService.list(loanChannelFormField);
        ExcelUtil<LoanChannelFormField> util = new ExcelUtil<LoanChannelFormField>(LoanChannelFormField.class);
        util.exportExcel(response, list, "渠道关联动态单组数据");
    }

    /**
     * 获取渠道关联动态单组详细信息
     */
    @PreAuthorize("@ss.hasPermi('channel:form:field')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(loanChannelFormFieldService.getById(id));
    }

    /**
     * 新增渠道关联动态单组
     */
    @PreAuthorize("@ss.hasPermi('channel:form:field')")
    @Log(title = "渠道关联动态单组", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LoanChannelFormField loanChannelFormField) {
        return toAjax(loanChannelFormFieldService.insertLoanChannelFormField(loanChannelFormField));
    }

    /**
     * 修改渠道关联动态单组
     */
    @PreAuthorize("@ss.hasPermi('channel:form:field')")
    @Log(title = "渠道关联动态单组", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LoanChannelFormField loanChannelFormField) {
        return toAjax(loanChannelFormFieldService.updateLoanChannelFormField(loanChannelFormField));
    }

    /**
     * 新增渠道关联动态单组
     */
    @PreAuthorize("@ss.hasPermi('channel:form:field')")
    @Log(title = "渠道关联动态单组", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult addTree(@RequestBody LoanChannelFormField loanChannelFormField) {
        return toAjax(loanChannelFormFieldService.addTree(loanChannelFormField));
    }

    /**
     * 删除渠道关联动态单组
     */
    @PreAuthorize("@ss.hasPermi('channel:form:field')")
    @Log(title = "渠道关联动态单组", businessType = BusinessType.DELETE)
    @PostMapping("/delete")
    public AjaxResult delete(@RequestBody List<LoanChannelFormField> ids) {
        return toAjax(loanChannelFormFieldService.delete(ids));
    }

    /**
     * 删除渠道关联动态单组
     */
    @PreAuthorize("@ss.hasPermi('channel:form:field')")
    @Log(title = "渠道关联动态单组", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(loanChannelFormFieldService.batchDeleteById(ids));
    }
}
